package com.app.sanyou.view.viewpager;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.sanyou.R;
import com.app.sanyou.entity.DataVo;
import com.app.sanyou.utils.StringUtil;
import com.app.sanyou.utils.UserUtil;
import com.app.sanyou.view.adapter.DataListAdapter;
import com.app.sanyou.view.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class DataFragment extends Fragment  {

    private static final String TAG = "DataFragment";

    private RecyclerView rv;

    private ImageView back_img;
    private TextView back_text;
    private TextView title_text;

    private Context context;
    private FragmentManager fragmentManager;

    private String userId;



    private Handler handler = new Handler();
    private DataListAdapter dataListAdapter;

    public static DataFragment getInstance(Context context, FragmentManager fragmentManager){
        DataFragment dataFragment = new DataFragment();
        dataFragment.context = context;
        dataFragment.fragmentManager = fragmentManager;
        return dataFragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.data_fragment,null);

        //初始化view
        initView(view);

        //判断用户登录是否过期
        userId = UserUtil.getUserId(context);
        if(StringUtil.isNull(userId)){
            UserUtil.loginOut(context);
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
        }
        //暂时先写死
        List<DataVo> dataSource=new ArrayList<>();
        DataVo dataVo = new DataVo();
        dataVo.dataTypeName="平面度数据";
        dataVo.dataTypeCode=1;
        dataSource.add(dataVo);
        dataVo = new DataVo();
        dataVo.dataTypeName="铜粒子数据";
        dataVo.dataTypeCode=2;
        dataSource.add(dataVo);
        dataListAdapter.setDataSource(dataSource);
        return view;
    }

    private void initView(View view){
        rv = view.findViewById(R.id.rv);
        dataListAdapter = new DataListAdapter(context);
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(dataListAdapter);

        //初始化顶部栏
        back_img = view.findViewById(R.id.back_img);
        back_text = view.findViewById(R.id.back_text);
        title_text = view.findViewById(R.id.title_text);
        //隐藏返回按钮和文字
        back_img.setVisibility(View.GONE);
        back_text.setVisibility(View.GONE);
        title_text.setText("数据中心");
    }
}
